package com.winkey.facility.gateway.service.parking.impl.callback;

import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.winkey.facility.gateway.service.parking.impl.jna.EYESTNETLIBLibrary;

import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * @author Administrator
 */
public class NET_SMARTRECVCALLBACK implements EYESTNETLIBLibrary.NET_SMARTRECVCALLBACK
{
    @Override
    public void apply(EYESTNETLIBLibrary.NET_DEV_SMARTRECRESUTLIST.ByReference SmartResult,
                      Pointer pJpeg,
                      IntByReference nLength,
                      Pointer userdata,
                      Pointer UserParam)
    {
        System.out.println("Enter call back func.");
        System.out.println("Camip = " + new String(SmartResult.DevIP.IpV4));
        String PlateNum = null;
        try {
            PlateNum = new String(SmartResult.parkspaceresult[0].plateresult.platenum,"GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("Plate = " + PlateNum);
        String PlateColor = null;
        try {
            PlateColor = new String(SmartResult.parkspaceresult[0].plateresult.platecolor, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("Plate color = " + PlateColor);
        System.out.println("Plate pic leng = " + nLength.getValue());
        System.out.println("bottom = " + SmartResult.parkspaceresult[0].plateresult.platerect.bottom);
        System.out.println("top = " + SmartResult.parkspaceresult[0].plateresult.platerect.top);
        System.out.println("left = " + SmartResult.parkspaceresult[0].plateresult.platerect.left);
        System.out.println("right = " + SmartResult.parkspaceresult[0].plateresult.platerect.right);

        /* 保存图片 */
        if(null != pJpeg){
            String path = ClassLoader.getSystemResource("").getPath()+ "Test.jpg";
            System.out.println("path = " + path);
            byte[]pImg = pJpeg.getByteArray(0,nLength.getValue());
            byte2image(pImg,path);
            //showImage(path,SmartResult.imgwidth,SmartResult.imgheight,lblCarPic);
        }
    }

    public void byte2image(byte[] data,String path){
        if(data.length<3||path.equals("")) {
            return;
        }
        try{
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        } catch(Exception ex) {
            System.out.println("Exception: " + ex + path );
            ex.printStackTrace();
        }
    }
}
